package g2.routemaster.gui;

import g2.routemaster.bundle.gui.GuiAdaptor;
import g2.routemaster.gui.contents.GameBoardComposite;
import g2.routemaster.gui.contents.PlayersWaitingGroup;
import g2.routemaster.model.GameManager;

import org.eclipse.jface.action.MenuManager;
import org.eclipse.jface.window.ApplicationWindow;
import org.eclipse.swt.SWT;
import org.eclipse.swt.layout.FillLayout;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Control;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;

public class ApplicationStart extends ApplicationWindow {
   private static final String APP_NAME = "Routemaster";
   private GameMenuManager menuManager;
   private Composite contents;
   PlayersWaitingGroup waitingGroup;
   GameBoardComposite boardContents;
   private static final int SIZE_X = 1024;
   private static final int SIZE_Y = 700;

   
   /**
    * ApplicationStart constructor
    */
   public ApplicationStart(GuiAdaptor adaptor) {
      super(null);

      ActionCollection.instance().initialize(this);

      // menu
      addMenuBar();

      // create GameManager
      GManager.instance();
   }

   /**
    * Runs the application
    */
   public void run() {
      // Don't return from open() until window closes
      setBlockOnOpen(true);

      // Open the main window
      open();

      // Dispose the display
      Display.getCurrent().dispose();
   }

   public void createWaitingGroup(){
   	if(contents.getChildren().length > 0){
         contents.getChildren()[0].dispose();
      }
   	
   	waitingGroup = new PlayersWaitingGroup(contents, SWT.NONE);
      waitingGroup.create();
      waitingGroup.setLayout(new GridLayout());
      waitingGroup.setLayoutData(new GridData(GridData.FILL_BOTH));
   }

   public void updateWaitingList(){
      waitingGroup.updateData();
      updateSize();
   }

   public void createGamingBoard(){
      if(contents.getChildren().length > 0){
         contents.getChildren()[0].dispose();
      }

      boardContents = new GameBoardComposite(contents, SWT.NONE);
      boardContents.create();
      boardContents.setLayout(new FillLayout());
      updateSize();
   }

   protected Control createContents(Composite parent) {
      GManager.instance().setWindow(this);
      contents = new Composite(parent, SWT.NONE);
      contents.setLayout(new FillLayout());

      createWaitingGroup();
      /*
      initActions();
      //updateWaitingList();
      createGamingBoard();
      */
      return contents;
   }

   void initActions(){
      GameManager gm = GManager.instance().getGameManager();
      // add players
      gm.insertPlayer("Pam-Pam", 41, "(51,51,255)");
      gm.insertPlayer("Ulrich", 18, "(255,51,51)");
      //gm.insertPlayer("Alibaba-Dudadada", 34, "(255,255,153)");
      //gm.insertPlayer("Mr. Mackey", 34, "(51,255,51)");

      // 2 cards to players hand
      gm.startGame();
   }
   
   /**
    * The application entry point
    * 
    * @param args the command line arguments
    */
   public static void main(String[] args) {
     new ApplicationStart(null).run();
   }


   /**
    * Create menu manager
    */
   protected MenuManager createMenuManager() {
      menuManager = new GameMenuManager();
      menuManager.create();
      return menuManager.getManager();
   }

   /**
    * Configure shell
    */
   protected void configureShell(Shell shell) {
      super.configureShell(shell);
      shell.setText(APP_NAME);
      shell.setSize(SIZE_X, SIZE_Y);
   }

   /**
    * window was closed from X
    */
   protected void handleShellCloseEvent() {
      ActionCollection.instance().getExitAction().run();
   }

   void updateSize(){
      getShell().setSize(getShell().getSize().x, getShell().getSize().y+1);
      getShell().setSize(getShell().getSize().x, getShell().getSize().y-1);
   }
}
